package com.push.spider.spider;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.push.common.config.MinioConfig;
import com.push.common.config.TransConfig;
import com.push.common.manager.BeanManager;
import com.push.common.utils.*;
import com.push.common.utils.httpclient.HttpClientResult;
import com.push.common.utils.httpclient.HttpClientUtil;
import com.push.spider.utils.UrlUtils;
import com.push.web.domain.BizArticle;
import com.push.web.domain.BizLabelSecond;
import com.push.web.domain.BizSource;
import com.push.web.service.IBizArticleAppendixService;
import com.push.web.service.IBizArticleService;
import com.push.web.service.IBizLabelSecondService;
import com.push.web.service.IBizSourceService;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

public class NasaReleasesSpider {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private ArrayList<String> hasDownUrls = new ArrayList<>();

    private Queue<String> queue = new LinkedList<String>();

    private BizSource source;

    private List<BizLabelSecond> labelSeconds = new ArrayList<>();

    @Autowired
    IBizArticleService articleService = BeanManager.getBean(IBizArticleService.class);

    @Autowired
    IBizSourceService sourceService = BeanManager.getBean(IBizSourceService.class);

    @Autowired
    IBizLabelSecondService labelSecondService = BeanManager.getBean(IBizLabelSecondService.class);

    @Autowired
    IBizArticleAppendixService articleAppendixService =
            BeanManager.getBean(IBizArticleAppendixService.class);

    @Autowired
    MinioUtil minioUtil = BeanManager.getBean(MinioUtil.class);

    @Autowired
    MinioConfig prop = BeanManager.getBean(MinioConfig.class);

    @Autowired
    private TransConfig transConfig = BeanManager.getBean(TransConfig.class);

    public int size = 0;

    public NasaReleasesSpider(String sourceId) {
        this.source = sourceService.selectBizSourceBySourceId(Long.valueOf(sourceId));
        this.labelSeconds =
                labelSecondService.selectBizLabelSecondListByParentIds(this.source.getLabelIds());
    }

    @SneakyThrows
    public void run() {
        HashMap params = new HashMap();
        params.put("size", "24");
        params.put("from", "0");
        params.put("sort", "promo-date-time:desc");
        params.put(
                "q",
                "((ubernode-type:press_release) AND (collections:5105))");
        params.put(
                "_source_include",
                "promo-date-time,master-image,nid,title,topics,missions,collections,other-tags,ubernode-type,primary-tag,secondary-tag,cardfeed-title,type,collection-asset-link,link-or-attachment,pr-leader-sentence,image-feature-caption,attachments,uri");
        HttpClientResult personResult = new HttpClientResult();
        personResult =
                HttpClientUtil.doGet("https://www.nasa.gov/api/2/ubernode/_search", params, true);
        JSONObject resultObj = JSONObject.parseObject(personResult.getContent());
        JSONArray jsonArray = resultObj.getJSONObject("hits").getJSONArray("hits");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject articleObj = jsonArray.getJSONObject(i);
            String titleOrg = articleObj.getJSONObject("_source").getString("title");
            String avator =
                    articleObj.getJSONObject("_source").getJSONObject("master-image").getString("uri");
            if (StringUtils.isNotEmpty(avator)) {
                avator = avator.replaceAll("public://", "https://www.nasa.gov/sites/default/files/");
            }
            String sourceUrl =
                    UrlUtils.buildUrl(
                            "https://www.nasa.gov/", articleObj.getJSONObject("_source").getString("uri"));
            String publishTime = articleObj.getJSONObject("_source").getString("promo-date-time");
            String articleId = articleObj.getJSONObject("_source").getString("nid");

            HttpClientResult detailResult =
                    HttpClientUtil.doGet("https://www.nasa.gov/api/2/ubernode/" + articleId, true);
            JSONObject detailObj = JSONObject.parseObject(detailResult.getContent());
            String content = detailObj.getJSONObject("_source").getString("body");
            String dataKey = EncryptUtil.encrypt(sourceUrl, "md5");
            BizArticle article =
                    buildArticle(
                            dataKey, titleOrg, sourceUrl, avator, publishTime, content, source.getSourceId());
            BizArticle bizArticle = articleService.selectBizArticleByDataKey(dataKey);
            if (StringUtils.isNull(bizArticle)) {

                if (strContains(article.getTitleOrg(), labelSeconds)) {
                    //自动翻译标题
                    if (StringUtils.isContainsChinese(article.getTitleOrg())){
                        article.setTitleCn(article.getTitleOrg());
                    } else {
                        String transWord = getTransResult(article.getTitleOrg(), "auto", "zh");
                        JSONObject titleObj = JSONObject.parseObject(transWord);
                        JSONArray titleArray = titleObj.getJSONArray("trans_result");
                        if (StringUtils.isNotNull(titleArray) && titleArray.size() > 0){
                            article.setTitleCn(titleArray.getJSONObject(0).getString("dst"));
                        }
                    }
                    //自动翻译摘要
                    if(transConfig.isAbstractTrans() && StringUtils.isNotEmpty(article.getSummaryOrg())){
                        String transWord = getTransResult(article.getSummaryOrg(), "auto", "zh");
                        JSONObject titleObj = JSONObject.parseObject(transWord);
                        JSONArray titleArray = titleObj.getJSONArray("trans_result");
                        if (StringUtils.isNotNull(titleArray) && titleArray.size() > 0){
                            article.setSummaryCn(titleArray.getJSONObject(0).getString("dst"));
                        }
                    }
                    articleService.insertBizArticle(article);
                }
            } else {
                article.setId(bizArticle.getId());
                articleService.updateBizArticle(article);
            }
            size++;
            Thread.sleep(source.getIntervalNum() * 1000);
        }
    }

    public BizArticle buildArticle(
            String dataKey,
            String titleOrg,
            String url,
            String avator,
            String publishTime,
            String content,
            long sourceId) {
        BizArticle article = new BizArticle();
        article.setDataKey(dataKey);
        article.setTitleOrg(titleOrg);
        article.setUrl(url);
        article.setAvator(avator);
        article.setPublishTime(publishTime);
        article.setPublishFormatTime(DateUtils.formatDate(DateUtils.parseDate(publishTime),publishTime));
        article.setContentsOrg(content);
        article.setSourceId(sourceId);
        article.setCreateTime(DateUtils.getNowDate());
        return article;
    }

    public BizArticle updateArticle(
            String dataKey,
            String summaryOrg,
            String contentsOrg,
            String publisher,
            String publishTime,
            String avator,
            long sourceId) {
        BizArticle article = new BizArticle();
        article.setDataKey(dataKey);
        article.setSummaryOrg(summaryOrg);
        contentsOrg.replaceAll("visibility: hidden;", "visibility: visible;");
        article.setContentsOrg(contentsOrg);
        article.setContentsCn(StringUtils.delHtmlTags(contentsOrg));
        article.setPublisher(publisher);
        article.setPublishTime(publishTime);
        article.setPublishFormatTime(DateUtils.formatDate(DateUtils.parseDate(publishTime),publishTime));
        article.setAvator(avator);
        article.setSourceId(sourceId);
        article.setCreateTime(DateUtils.getNowDate());
        return article;
    }

    public static boolean strContains(String str, List<BizLabelSecond> labels) {
        if (labels == null) {
            return true;
        }
        if (labels.isEmpty()) {
            return true;
        }
        if (StringUtils.isEmpty(str)) {
            return false;
        }
        long len =
                labels.parallelStream()
                        .filter(
                                label ->
                                        str.toLowerCase(Locale.ROOT)
                                                .contains(label.getLabelName().toLowerCase(Locale.ROOT)))
                        .count();
        if (len > 0) {
            return true;
        }
        return false;
    }

    public String getTransResult(String query, String from, String to) {
        Map<String, String> params = buildParams(query, from, to);
        return HttpClientUtil.doGet(transConfig.getTransUrl(), params,false).getContent();
    }

    private Map<String, String> buildParams(String query, String from, String to) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("q", query);
        params.put("from", from);
        params.put("to", to);
        params.put("appid", transConfig.getAppid());
        // 随机数
        String salt = String.valueOf(System.currentTimeMillis());
        params.put("salt", salt);
        // 签名
        String src = transConfig.getAppid() + query + salt + transConfig.getSecurityKey();
        params.put("sign", MD5.md5(src));
        return params;
    }

    public int getSize() {
        return size;
    }
}
